package com.hibernate.dao.implement;

import java.util.List;
import java.util.ArrayList;

import org.apache.log4j.Logger;
//import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
//import org.hibernate.criterion.Restrictions;

import com.hibernate.dao.DAOFuenteInformacion;
import com.hibernate.model.FuenteInformacion;

/***
 * 
 * @author LISETH JAZMIN RENDON ALVAREZ
 * @version 1.0
 * 
 * Clase que implementa los metodos de la interfaz DAOFuenteInformacion
 * @see DAOFuenteInformacion
 *
 */

public class DAOFuenteInformacionImpl implements DAOFuenteInformacion {

	private static Logger logger = Logger.getLogger(DAOFuenteInformacionImpl.class);
	
	/* (non-Javadoc)
	 * @see com.hibernate.dao.DAOFuenteInformacion#obtenerFuentes()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<FuenteInformacion> obtenerFuentes() {
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 
		List<FuenteInformacion> encontradas = new ArrayList<FuenteInformacion>();
		
		try {
			encontradas = session.createQuery("from FuenteInformacion").list();
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}
		
		return encontradas;
	}

	/* (non-Javadoc)
	 * @see com.hibernate.dao.DAOFuenteInformacion#obtenerFuente(String)
	 */
	@Override
	public FuenteInformacion obtenerFuente(String codigo) {
	
		Session session = HibernateUtil.getSessionFactory().openSession();
		FuenteInformacion encontrada = null;
		
		try {
			encontrada = (FuenteInformacion) session.get(FuenteInformacion.class, codigo);
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}
		
		return encontrada;
	}

	/* (non-Javadoc)
	 * @see com.hibernate.dao.DAOFuenteInformacion#guardarFuente(com.hibernate.model.FuenteInformacion)
	 */
	@Override
	public void guardarFuente(FuenteInformacion fuente) {
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 
		session.beginTransaction();
		
		try {
			session.save(fuente);
			session.getTransaction().commit();
			
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}		
	}

	/* (non-Javadoc)
	 * @see com.hibernate.dao.DAOFuenteInformacion#eliminarFuente(com.hibernate.model.FuenteInformacion)
	 */
	@Override
	public void eliminarFuente(FuenteInformacion fuente) {
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 
		session.beginTransaction();
		
		try {
			session.delete(fuente);
			session.getTransaction().commit();
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}
		
	}

	/* (non-Javadoc)
	 * @see com.hibernate.dao.DAOFuenteInformacion#editarFuente(com.hibernate.model.FuenteInformacion)
	 */
	@Override
	public void editarFuente(FuenteInformacion fuente) {
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 
		session.beginTransaction();
		
		try {
			session.merge(fuente);
			session.getTransaction().commit();
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}
		
	}

}
